﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;

namespace PcProject.Infrastructure.Repository
{
    /// <summary>
    /// 商品分类实现类
    /// </summary>
    public class ProCateRepository:DapperRepositoryBase<ProductCategory>, IProCateRepository
    {
        //构造函数依赖注入
        public IConfiguration _configuration;
        //函数依赖注入
        public ProCateRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }


        /// <summary>
        /// 商品分类显示分页
        /// </summary>
        /// <param name="pname">查询参数</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public List<ProductCategory> GetProCatesAsync(/*int pageIndex, int pageSize, out int totalCount*/)
        {
            string sql = $"select * from ProductCategory";
            //sql += $" limit @p1,@p2";
            ////计算条数
            //string sqlcount = $"select count(*) from ProductCategory";
            ////总条数
            //totalCount = Conn.ExecuteScalar<int>(sqlcount);
            return Conn.Query<ProductCategory>(sql/*, new { p1 = (pageIndex - 1) * pageSize, p2 = pageSize }*/).ToList();
        }

        /// <summary>
        /// 商品分类添加
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> InserAsync(ProductCategory obj)
        {
            return (int)await Conn.InsertAsync(obj);
        }

        /// <summary>
        /// 商品分类删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>

        public async Task<int> DeleAsync(int id)
        {
            string sql = $"delete from ProductCategory where CateId={id}";
            return await Conn.ExecuteAsync(sql);
        }

        /// <summary>
        /// 商品分类反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<ProductCategory> FillAsync(int id)
        {
            return await Conn.GetAsync<ProductCategory>(id);
        }

        /// <summary>
        /// 商品分类修改
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public async Task<int> UptAsync(ProductCategory obj)
        {
            return await Conn.UpdateAsync(obj);
        }

    }
}
